from django.shortcuts import render, HttpResponse, redirect

from utils.tencent.cos import delete_bucket
from web import models

def setting(request, project_id):
    """ 设置 """
    return render(request, 'setting.html')


def setting_delete(request, project_id):
    """ 删除项目 """
    if request.method == 'GET':
        return render(request, 'setting_dalete.html')

    project_name = request.POST.get('project_name')
    print(project_name)
    if not project_name or project_name != request.bug_platform.project.name:
        return render(request, 'setting_dalete.html', {'error': '项目名错误'})

    # 项目名正确 删除【只有创建者才能删除
    if request.bug_platform.user != request.bug_platform.project.creator:
        return render(request, 'setting_dalete.html', {'error': '你不是项目创建者，只有项目创建者才能删除项目'})

    # 1，删除桶
    # 不能直接删除桶/要把桶中的文件先删除
    #   -- 删除桶中所有的文件【找到所有文件 + 删除文件
    #   -- 删除桶中所有的文件【找到碎片 + 删除碎片
    #   -- 删除桶
    # 2，删除项目
    #   -- 数据库项目删除
    delete_bucket(request.bug_platform.project.bucket, request.bug_platform.project.region)
    # 删除桶
    models.Project.objects.filter(id=request.bug_platform.project.id).delete()

    return redirect('project_list')
